<!--
 * @Description: 企业主财务明细
 * @Author: Li Yujie
 * @Date: 2021/05/15
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2021/05/15
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        公司名称
                    </p>
                    <el-input
                        placeholder="请输入公司名称"
                        v-model="filterData.hire_company_name"
                        suffix-icon="el-icon-search"
                        clearable
                        style="width: 203px"
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        公司编号
                    </p>
                    <el-input
                        placeholder="请输入公司编号"
                        v-model="filterData.keyword_hire_id"
                        suffix-icon="el-icon-search"
                        clearable
                        style="width: 203px"
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        公司手机号
                    </p>
                    <el-input
                        placeholder="请输入公司手机号"
                        v-model="filterData.hire_username"
                        suffix-icon="el-icon-search"
                        clearable
                        style="width: 203px"
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        账单类型
                    </p>
                    <el-select
                        v-model="filterData.type"
                        filterable
                        placeholder="全部"
                        size="medium"
                        style="width: 152px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in typeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        收支类型
                    </p>
                    <el-select
                        v-model="filterData.money_type"
                        filterable
                        placeholder="全部"
                        size="medium"
                        style="width: 152px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in moneyTypeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        账单状态
                    </p>
                    <el-select
                        v-model="filterData.status"
                        filterable
                        placeholder="全部"
                        size="medium"
                        style="width: 152px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        创建时间
                    </p>
                    <el-date-picker
                        v-model="dateTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="medium"
                        style="width: 353px"
                        value-format="timestamp"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                    <el-button
                        type="primary"
                        size="small"
                        @click="handleReset()"
                    >
                        重置
                    </el-button>
                    <el-button
                        type="primary"
                        size="small"
                        @click="handleExport()"
                    >
                        导出搜索记录
                    </el-button>
                </div>
            </div>
            <!--筛选-->

            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
                :cell-style="{height: '105px'}"
                ref="tempLateName"
                class="data-table"
            >
                <el-table-column
                    label="序号"
                    align="center"
                    width="60"
                    type="index"
                    fixed="left"
                />
                <el-table-column
                    label="企业信息"
                    width="220"
                    fixed="left"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div class="cell-title">
                            <div class="user-name">{{ scope.row.hire_info.company_name ? scope.row.hire_info.company_name : '--' }}</div>
                        </div>
                        <div class="cell-tips">手机号：{{ scope.row.hire_info.username }}</div>
                        <div class="cell-tips">企业编号：{{ scope.row.hire_info.hire_no }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="order_no"
                    label="账单编号"
                    width="200"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="账单类型"
                    align="center"
                    width="120"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.type | typeFilter }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="title"
                    label="账单标题"
                    width="120"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="收支类型"
                    align="center"
                    width="120"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.money_type | moneyTypeFilter }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="账单金额"
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        ￥{{ scope.row.money }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="手续费"
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        ￥{{ scope.row.commission }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="账单创建时间"
                    width="200"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="账单状态"
                    align="center"
                    width="120"
                >
                    <template slot-scope="scope">
                        <div class="status-wrapper">
                            <div
                                :class="['status-dot', scope.row.status === 1 ? 'warning' : (scope.row.status === 3 ? 'success' : scope.row.status === 2 ? 'danger' : 'normal')]"/>
                            <div>{{ scope.row.status | statusFilter }}</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="complete_remark"
                    label="完成备注"
                    width="130"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="remark"
                    label="账单备注"
                    show-overflow-tooltip
                    min-width="120"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="操作"
                    align="left"
                    width="180"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <div class="option-btn-group">
                            <el-button
                                :disabled="!authResult.auth_hire || scope.row.status !== 1"
                                type="text"
                                @click="handleAutoComplete(scope.row.order_no)">自动打款</el-button>
                            <el-button
                                type="text"
                                :disabled="scope.row.status !== 1"
                                @click="showDialog('complete', scope.row.order_no)"
                            >
                                完成
                            </el-button>
                            <el-button
                                :disabled="scope.row.status !== 1"
                                type="text"
                                class="btn-delete"
                                @click="showDialog('reject',scope.row.order_no)"
                            >
                                拒绝
                            </el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!-- 未完成原因弹窗 -->
        <reject-dialog
            title="请输入拒绝原因"
            :visible.sync="rejectDialog"
            :maxlength="100"
            placeholder="请输入拒绝原因"
            @confirm="handleSetStatus(order_no, 3, '', $event)"
        />

        <!-- 完成原因弹窗 -->
        <reject-dialog
            title="请输入交易订单编号"
            :visible.sync="completeDialog"
            :maxlength="100"
            placeholder="交易订单编号"
            @confirm="handleSetStatus(order_no, 1, $event)"
        />
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import rejectDialog from '@/components/dialog/reject-dialog';

export default {
    name: 'FinanceShopList',
    mixins: [back],
    components: {
        rejectDialog,
    },
    data() {
        return {
            filterData: {
                title: '',
                type: 0,
                money_type: 0,
                status: 0,
                hire_company_name: '',
                keyword_hire_id: '',
                hire_username: '',
                start_time: '',
                end_time: '',
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            // 账单类型
            typeList: [
                {value: 0, label: '全部'},
                {value: 1, label: '商品购买'},
                {value: 2, label: '提现'},
                {value: 3, label: '分佣'},
            ],
            moneyTypeList: [
                {value: 0, label: '全部'},
                {value: 1, label: '收入'},
                {value: 2, label: '支出'},
                {value: 101, label: '冻结'}
            ],
            // 账单状态
            statusList: [
                {value: 0, label: '全部'},
                {value: 1, label: '进行中'},
                {value: 2, label: '终止'},
                {value: 3, label: '已完成'},
                {value: -1, label: '未开始'}
            ],
            authResult: {},
            order_no: '',
            rejectDialog: false,
            completeDialog: false
        };
    },
    computed: {},
    watch: {
        dateTimeRange(value) {
            this.filterData.start_time = value ? value[0] / 1000 : '';
            this.filterData.end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },
    filters: {
        typeFilter(val) {
            switch (val) {
                case 1:
                    return '商品购买';
                case 2:
                    return '提现';
                case 3:
                    return '分佣';
                case 4:
                    return '保证金';
                default:
                    return '--';
            }
        },
        moneyTypeFilter(val) {
            switch (val) {
                case 1:
                    return '收入';
                case 2:
                    return '支出';
                case 101:
                    return '冻结';
                default:
                    return '--';
            }
        },
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '未开始';
                case 1:
                    return '进行中';
                case 2:
                    return '已终止';
                case 3:
                    return '已完成';
                default:
                    return '--';
            }
        }
    },

    mounted() {
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/finance_shop%5Cget_list', params, async res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 审核
         * @param order_no
         * @param type 1完成 2自动打款 3拒绝
         * @param complete_remark 完成原因
         * @param refuse_reason 拒绝原因
         */
        handleSetStatus(order_no, type, complete_remark = '', refuse_reason = '') {
            let params = {
                order_no,
                type,
                complete_remark,
                refuse_reason
            };

            this.$post('/student/finance_shop%5Cset_status', params, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 显示弹窗
         * @param type
         * @param order_no
         */
        showDialog(type, order_no) {
            this.order_no = order_no;
            this[`${type}Dialog`] = true;
        },

        /**
         * 自动打款
         * @param order_no
         */
        handleAutoComplete(order_no) {
            this.$confirm('此操作自动打款, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.handleSetStatus(order_no, 2);
            });
        },

        /**
         * 重置表单
         */
        handleReset() {
            this.filterData = {
                title: '',
                type: 0,
                money_type: 0,
                status: 0,
                hire_company_name: '',
                keyword_hire_id: '',
                hire_username: '',
                start_time: '',
                end_time: '',
            };

            this.dateTimeRange = [];
            this.getTableData();
        },

        /**
         * 导出数据
         */
        handleExport() {
            let { title, type, money_type, status, start_time, end_time, keyword_hire_id, hire_company_name } = this.filterData;
            let base = this.$baseUrl;
            let ticket = localStorage.getItem('sessionTicket');
            let url = `${base}/student/finance_shop%5Cexport_list?type=${type}&money_type=${money_type}&status=${status}&start_time=${start_time}&end_time=${end_time}&keyword_hire_id=${keyword_hire_id}&hire_company_name=${hire_company_name}&__ticket=${ticket}`;

            window.open(url);
        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;
    flex: 1;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        padding: 0 24px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin-left: 12px;
                margin-bottom: 24px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            margin-top: 12px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 12px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }

            .input-with-select {
                width: 300px;
                margin-right: 32px;
            }

            /deep/ .input-with-select .el-input-group__prepend {
                background-color: #fff;
                width: 130px;
            }
        }

        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            .cell-title {
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                margin-bottom: 12px;
                display: flex;
                align-items: center;
                .user-name {
                    max-width: 124px;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
                .user-tags {
                    display: flex;
                    align-items: center;
                    margin-left: 6px;
                    .tag-item {
                        margin-right: 4px;
                        display: flex;
                        align-items: center;
                        img {
                            height: 14px;
                        }
                    }
                }
            }
            .cell-content {
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 3;
            }
            .cell-tips {
                color: #999;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
                .btn-delete {
                    color: #F95B56;
                    &.is-disabled {
                        color: #c0c4cc;
                        cursor: not-allowed;
                    }
                }
            }
            .status-wrapper {
                display: flex;
                align-items: center;
                justify-content: center;

                .status-dot {
                    width: 6px;
                    height: 6px;
                    border-radius: 50%;
                    margin-right: 5px;

                    &.success {
                        background: #4CD964;
                    }

                    &.danger {
                        background: #FF3030;
                    }

                    &.warning {
                        background: #FFA72D;
                    }

                    &.normal {
                        background: #C2C2C2;
                    }
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-table__fixed-right {
        right: 6px!important;
    }
    /deep/ .el-dialog__footer {
        text-align: center;
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>
